package com.mask.im.common.model;
import com.mask.im.common.enums.StatusCodeEnum;
import lombok.Builder;
import lombok.Data;

/**
 * @author jian
 */
@Data
@Builder
public class RestResponse<T> {

	private Boolean flag;

	private Integer code;

	private String message;

	private T data;

	public static <T> RestResponse<T> ok(){
		return resultVO(true, StatusCodeEnum.SUCCESS.getCode(), StatusCodeEnum.SUCCESS.getDesc(),null);
	}

	public static <T> RestResponse<T> ok(T data){
		return resultVO(true, StatusCodeEnum.SUCCESS.getCode(), StatusCodeEnum.SUCCESS.getDesc(),data);
	}

	public static <T> RestResponse<T> okData(T data){
		return resultVO(true, StatusCodeEnum.SUCCESS.getCode(), StatusCodeEnum.SUCCESS.getDesc(),data);
	}

	public static <T> RestResponse<T> ok(String message){
		return resultVO(true,message);
	}

//	public static <T> ResultVO<T> ok(Integer code,String message){
//		return resultVO(true,code,message,null);
//	}

	public static <T> RestResponse<T> fail(){
		return resultVO(false, StatusCodeEnum.FAIL.getCode(), StatusCodeEnum.FAIL.getDesc(),null);
	}

	public static <T> RestResponse<T> fail(T data){
		return resultVO(false, StatusCodeEnum.FAIL.getCode(), StatusCodeEnum.FAIL.getDesc(),data);
	}

	public static <T> RestResponse<T> fail(String message){
		return resultVO(false,message);
	}

	public static <T> RestResponse<T> fail(Integer code,String message){
		return resultVO(false,code,message,null);
	}

	private static <T> RestResponse<T> resultVO(Boolean flag,String message){
		return RestResponse.<T>builder()
				.flag(flag)
				.code(flag ? StatusCodeEnum.SUCCESS.getCode(): StatusCodeEnum.FAIL.getCode())
				.message(message)
				.build();
	}

	private static <T> RestResponse<T> resultVO(Boolean flag,Integer code,String message,T data){
		return RestResponse.<T>builder()
				.flag(flag)
				.code(code)
				.message(message)
				.data(data).build();
	}
}
